// 加载表格
window.onload = function () {
    // 获取id值
    var loc = location.href;
    var n1 = loc.length;//地址的总长度
    var n2 = loc.indexOf("=");//取得=号的位置
    var cityids = loc.substr(n2 + 1, n1 - 1);//从=号后面的内容
    var cityid = cityids.replace('#', '')
    console.log(cityid + "chengqu1/######");
    // 赋值给隐藏的
    $("#cityId").val(cityid)
    var url = 'http://192.168.0.102:8080/BusinessCity/getAreaByCityID'
    function s(datas) {
        function operateFormatter(value, row, index) {//赋予的参数
            return [
                '<button class="btn btn-primary" style="margin-right:10px"  onclick="editcityArea(' + row.areaid + ')">编辑</button>'
            ].join('');
        }
        // 设置table状态列样式
        function formatFormatter(value, row, index) {
            if (value == 1) {

                return [

                    '<img src="../../../images/success.png" onclick="statusChange(' + row.validflag + ',' + row.areaid + ')">',
                ].join('');
            } else {
                return [

                    '<img src="../../../images/fail.png" onclick="statusChange(' + row.validflag + ',' + row.areaid + ')">',
                ].join('');
            }
        }
        var tableColumns = [
            {
                field: 'areaid',
                title: '城区ID',
                switchable: true
            }, {
                field: 'name',
                title: '名称',
                switchable: true
            },
            {
                field: 'validflag',
                title: '状态',
                formatter: formatFormatter
            }, {
                field: 'operate',
                title: '操作',
                formatter: operateFormatter //自定义方法，添加操作按钮
            }
        ];
        $("#cityArea").bootstrapTable('destroy'); //分页必须写
        $("#cityArea").bootstrapTable({
            cache: false,
            pagination: true,
            sidePagination: "client",  //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,   //初始化加载第一页，默认第一页
            pageSize: 5,   //每页的记录行数（*）
            pageList: [5, 10], //可供选择的每页的行数
            columns: tableColumns,
            data: datas,
            queryParamsType: '',
            //得到查询的参数
            queryParams: function queryParams(params) {
                return params;
            },
            onLoadSuccess: function (data) {  //加载成功时执行
                alert("成功" + data)
            },
            onLoadError: function () {  //加载失败时执行
                alert("失败")
            }

        });
    }
    $.ajax({
        url: url,
        method: 'post',
        contentType: "application/x-www-form-urlencoded",
        data: { cityID: cityid },
        success: function (res) {
            console.log(res.data)
            s(res.data)

        }
    })
}
// 城区编辑返回
function backcitys(e) {
    // 返回
    divset = document.getElementsByClassName("search");
    for (var i = 0; i < divset.length; i++) {
        divset[i].style.display = "inline";
    };
    divsetedit = document.getElementsByClassName("editcitys");
    for (var i = 0; i < divset.length; i++) {
        divsetedit[i].style.display = "none";
    };
    divsetadd = document.getElementsByClassName("editAdds");
    for (var i = 0; i < divset.length; i++) {
        divsetadd[i].style.display = "none";
    };
}
// 编辑城区按钮
function editcityArea(areaid) {
    // 编辑
    divset = document.getElementsByClassName("search");
    console.log('name' + areaid)
    //var options=document.getElementById("editSelectOption").options;
    // 编辑赋值给编辑列表
    $.ajax({
        url: 'http://192.168.0.102:8080/BusinessCity/getEditAreaInfo',
        method: 'post',//请求方式
        contentType: "application/x-www-form-urlencoded",
        data: {
            areaID: areaid
        },
        success: function (data) {
            console.log(data)
            // 编辑的名字
            $("#editAreaName").val(data.data.name);
            //    赋值给隐藏的id
            $("#editareaid").val(data.data.areaid)
            console.log(data.data.areaid + "城区id")
        }
    })
    // 编辑
    divset = document.getElementsByClassName("search");
    for (var i = 0; i < divset.length; i++) {
        divset[i].style.display = "none";
    };
    divsetedit = document.getElementsByClassName("editcitys");
    for (var i = 0; i < divset.length; i++) {
        divsetedit[i].style.display = "inline";
    };
}
// 编辑城区保存
function editsaveCityArea() {
    // 获取值
    var editareaName = document.getElementById("editAreaName").value
    var editareaID = document.getElementById("editareaid").value
    console.log("name" + editareaName + "abab" + editareaID)
    // 后台请求
    $.ajax({
        url: 'http://192.168.0.102:8080/BusinessCity/updateArea',//请求后台的url
        method: 'post',//请求方式
        contentType: "application/x-www-form-urlencoded",
        data: {
            areaName: editareaName,
            areaID: editareaID
        },
        success: function (res) {
            console.log(res.msg)
            divset = document.getElementsByClassName("search");
            for (var i = 0; i < divset.length; i++) {
                divset[i].style.display = "inline";
                location.reload();
            }
            divsetedit = document.getElementsByClassName("editcitys");
            for (var i = 0; i < divset.length; i++) {
                divsetedit[i].style.display = "none";
            };
            divsetadd = document.getElementsByClassName("editAdds");
            for (var i = 0; i < divset.length; i++) {
                divsetadd[i].style.display = "none";
            }
        }
    })
}
//  添加城区按钮
function addcitys(e) {
    // 添加
    divset = document.getElementsByClassName("search");
    for (var i = 0; i < divset.length; i++) {
        divset[i].style.display = "none";
    };
    divsetadd = document.getElementsByClassName("editAdds");
    for (var i = 0; i < divset.length; i++) {
        divsetadd[i].style.display = "inline";
    };
}
// 添加保存城区
function addsaveCityarea() {
    //获取值
    var cityareaName = document.getElementById("areaName").value
    // 获取隐藏的cityId
    var cityid = document.getElementById("cityId").value
    $.ajax({
        url: 'http://192.168.0.102:8080/BusinessCity/addArea',
        method: 'post',
        contentType: "application/x-www-form-urlencoded",
        data: {
            cityID: cityid,
            areaName: cityareaName,
        },
        success: function (res) {
            console.log(res.data)
        }
    })
    // 返回
    divset = document.getElementsByClassName("search");
    for (var i = 0; i < divset.length; i++) {
        divset[i].style.display = "inline";
        location.reload();
    };
    divsetedit = document.getElementsByClassName("editcitys");
    for (var i = 0; i < divset.length; i++) {
        divsetedit[i].style.display = "none";
    };
    divsetadd = document.getElementsByClassName("editAdds");
    for (var i = 0; i < divset.length; i++) {
        divsetadd[i].style.display = "none";
    }
}
// 状态变化
// 城区状态按钮
function statusChange(validflag, areaid) {
    // 获取当前状态
    console.log(validflag);
    // 判断当前状态更改
    if (validflag === 1) {
        $('#modal-input').html('确认停用');
        $('#myModal').modal('show');
        var areastatus = 0
        console.log("dianji" + areastatus + areaid);
        changeStata(areastatus, areaid)
    }
    else {
        $('#modal-input').html('确认启用');
        $('#myModal').modal('show');
        var areastatus = 1
        console.log("dianji" + areastatus + areaid);
        changeStata(areastatus, areaid)
    }
}
//  城区状态确认更改按钮
// 切换状态
function changeStata(areastatus, areaid) {
    $('#btn_ok').click(function () {
        $.ajax({
            url: 'http://192.168.0.102:8080/BusinessCity/updateAreaStatus',
            method: 'post',
            contentType: 'application/x-www-form-urlencoded',
            data: {
                areaID: areaid,
                validFlag: areastatus
            },
            success: function (res) {
                console.log(res.msg);
                $('#myModal').modal('hide');
                location.reload();
            }
        })
    })
}